<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2124 – Assigning an array to a string! Assign as array, or use `*` instead of `@` to concatenate.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2124 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2124">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2
id="assigning-an-array-to-a-string-assign-as-array-or-use--instead-of--to-concatenate">Assigning
an array to a string! Assign as array, or use <code>*</code> instead of
<code>@</code> to concatenate.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2124.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Want to store multiple elements in var</span></span>
<span id="cb1-2"><a href="SC2124.html#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="va">var</span><span class="op">=</span><span class="va">$@</span></span>
<span id="cb1-3"><a href="SC2124.html#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> i <span class="kw">in</span> <span class="va">$var</span><span class="kw">;</span> <span class="cf">do</span> <span class="ex">..</span><span class="kw">;</span> <span class="cf">done</span></span></code></pre></div>
<p>or</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2124.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="bu">set</span> <span class="at">--</span> Hello World</span>
<span id="cb2-2"><a href="SC2124.html#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Want to concatenate multiple elements into a single string</span></span>
<span id="cb2-3"><a href="SC2124.html#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="va">msg</span><span class="op">=</span><span class="va">$@</span></span>
<span id="cb2-4"><a href="SC2124.html#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="bu">echo</span> <span class="st">&quot;You said </span><span class="va">$msg</span><span class="st">&quot;</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC2124.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Bash: use an array variable</span></span>
<span id="cb3-2"><a href="SC2124.html#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="va">var</span><span class="op">=</span><span class="va">(</span> <span class="st">&quot;</span><span class="va">$@</span><span class="st">&quot;</span> <span class="va">)</span></span>
<span id="cb3-3"><a href="SC2124.html#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> i <span class="kw">in</span> <span class="st">&quot;</span><span class="va">${var</span><span class="op">[@]</span><span class="va">}</span><span class="st">&quot;</span><span class="kw">;</span> <span class="cf">do</span> <span class="ex">..</span><span class="kw">;</span> <span class="cf">done</span></span>
<span id="cb3-4"><a href="SC2124.html#cb3-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-5"><a href="SC2124.html#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="co"># POSIX sh: without array support, one possible workaround</span></span>
<span id="cb3-6"><a href="SC2124.html#cb3-6" aria-hidden="true" tabindex="-1"></a><span class="co"># is to store elements concatenated with a delimiter (here linefeed/newline)</span></span>
<span id="cb3-7"><a href="SC2124.html#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="va">var</span><span class="op">=</span><span class="va">$(</span><span class="bu">printf</span> <span class="st">&#39;%s\n&#39;</span> <span class="st">&quot;</span><span class="va">$@</span><span class="st">&quot;</span><span class="va">)</span></span>
<span id="cb3-8"><a href="SC2124.html#cb3-8" aria-hidden="true" tabindex="-1"></a><span class="bu">printf</span> <span class="st">&#39;%s\n&#39;</span> <span class="st">&quot;</span><span class="va">$var</span><span class="st">&quot;</span> <span class="kw">|</span> <span class="cf">while</span> <span class="va">IFS</span><span class="op">=</span><span class="st">&#39;&#39;</span> <span class="bu">read</span> <span class="at">-r</span> <span class="va">line</span><span class="kw">;</span> <span class="cf">do</span> <span class="ex">..</span><span class="kw">;</span> <span class="cf">done</span></span></code></pre></div>
<p>or</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb4-1"><a href="SC2124.html#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co">#!/bin/sh</span></span>
<span id="cb4-2"><a href="SC2124.html#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="bu">set</span> <span class="at">--</span> Hello World</span>
<span id="cb4-3"><a href="SC2124.html#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Explicitly concatenates all the array elements into a single string</span></span>
<span id="cb4-4"><a href="SC2124.html#cb4-4" aria-hidden="true" tabindex="-1"></a><span class="va">msg</span><span class="op">=</span><span class="va">$*</span></span>
<span id="cb4-5"><a href="SC2124.html#cb4-5" aria-hidden="true" tabindex="-1"></a><span class="bu">echo</span> <span class="st">&quot;You said </span><span class="va">$msg</span><span class="st">&quot;</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>Arrays and <code>$@</code> can contain multiple elements. Simple
variables contain only one. When assigning multiple elements to one
element, the default behavior depends on the shell (bash concatenates
with spaces, zsh concatenates with first char of <code>IFS</code>).</p>
<p>Since doing this usually indicates a bug, ShellCheck warns and asks
you to be explicit about what you want.</p>
<p>If you want to assign N elements as N elements in Bash or Ksh, use an
array, e.g. <code>myArray=( "$@" )</code>.</p>
<p>Dash and POSIX sh do not support arrays. In this case, either
concatenate the values with some delimiter that you can split on later
(the example uses linefeeds and splits them back up with a
<code>while read</code> loop), or keep the values as positional
parameters without putting them in an intermediate variable.</p>
<p>If you want to assign N elements as 1 element by concatenating them,
use <code>*</code> instead of <code>@</code>, e.g.
<code>myVar=${myArray[*]}</code> (this separates elements with the first
character of <code>IFS</code>, usually space).</p>
<p>The same is true for <code>${@: -1}</code>, which results in 0 or 1
elements: <code>var=${*: -1}</code> assigns the last element or an empty
string.</p>
<h3 id="exceptions">Exceptions</h3>
<p>None.</p>
<h3
id="adding-an-element-to-an-array-may-trigger-this-warning-when-you-forget-the-parenthesis">Adding
an element to an array may trigger this warning when you forget the
parenthesis.</h3>
<pre><code>filelist=&quot;${filelist[@]}&quot; &quot;$filename&quot;</code></pre>
<p>What was meant is:</p>
<pre><code>filelist=(&quot;${filelist[@]}&quot; &quot;$filename&quot;)</code></pre>
<p>Note: This syntax is compatible with older shells; in later shells
(bash 3.1+ and zsh 4.2+), you can do
<code>filelist+=("$filename")</code>.</p>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


